<?php

/**
 * @Author: wuchenge
 * @Date:   2022-04-19 13:56:17
 * @Email: ligui0506@126.com
 * @Descript:
 */
namespace App\Admin\Controllers;

use App\Http\Controllers\ApiController;
use App\Models\Admin as AdminModel;
use App\Requests\AdminRequest;
use Illuminate\Http\Request;

class AdminController extends ApiController
{
    /**
     * @var mixed
     */
    protected $model;
    /**
     * @var mixed
     */
    protected $request;
    /**
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->model   = new AdminModel();
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $keyword = $request->input('keyword', '');
        $filter  = $request->input('filter', '');
        $sort    = $request->input('sort', '');
        $page    = $request->input('page', 1);
        $limit   = $request->input('limit', 10);

        $where = ['nomal' => []];
        if (!empty($keyword)) {
            $like = ['username', 'like', $keyword . '%'];
            array_push($where['nomal'], $like);
        }

        if (!empty($filter)) {
            $kv = explode('-', $filter);
            array_push($where['nomal'], $kv);
        }

        $order = [];
        if (!empty($sort)) {
            $kv            = explode('-', $sort);
            $order[$kv[0]] = $kv[1];
        }

        $field = ['*'];
        $data  = $this->model::lists(AdminModel::query(), $field, $where,
            $page,
            $limit, $order);
        return $this->response($data);
    }

    /**
     * @param Request $request
     */
    public function create(AdminRequest $request)
    {
        $status = $request->input('status')
        ? $this->model::STATUS_YES
        : $this->model::STATUS_NO;
        $password = $this->model::encodePassword(md5($request->input('pwd')));

        $admin = $this->model::store([
            'username'  => $request->input('username'),
            'nick_name' => $request->input('nick_name'),
            'pwd'       => $password,
            'status'    => $status,
        ]);

        if ($admin) {
            return $this->response();
        }

        return $this->failed();
    }

    /**
     * @param AdminRequest $request
     */
    public function update(AdminRequest $request, $id)
    {
        $status = $request->input('status')
        ? $this->model::STATUS_YES
        : $this->model::STATUS_NO;
        $pwd = $request->input('pwd');

        if ($this->model::isSupper($id)) {
            if ($status == $this->model::STATUS_NO) {
                $this->failed('超级管理员不能被禁用');
            }

            if ($id != $request->get('admin')->id) {
                $this->failed('超级管理员只能是自己修改');
            }
        }

        $modify = [
            'id'        => $id,
            'username'  => $request->input('username'),
            'nick_name' => $request->input('nick_name'),
            'status'    => $status,
        ];

        if (!empty($pwd)) {
            $modify['pwd'] = $this->model::encodePassword(md5($pwd));
        }

        $res = $this->model::modify($modify);
        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    /**
     * @return mixed
     */
    public function delete($id)
    {

        if ($this->model::isSupper($id)) {
            $this->failed('超级管理员不能被删除');
        }

        $res = $this->model::del($id);
        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    /**
     * @param $id
     */
    public function detail($id)
    {
        $admin = $this->model::find($id);
        return $this->response($admin);
    }

}
